<?php

namespace app\admin\controller;

use think\facade\Db;
use app\extend\Random;

class User extends Base
{

    protected function _infoModule()
    {
        return array(
            'info' => array(
                'name' => '会员管理',
                'description' => '管理网站后台管理员',
            ),
            'menu' => array(
                array(
                    'name' => '列表',
                    'url' => url('index'),
                    'icon' => 'list',
                ),
            ),
            '_info' => array(
                array(
                    'name' => '添加',
                    'url' => url('info'),
                ),
            ),
        );
    }

    public function index()
    {
        //筛选条件
        $where = array();

        $id = input('id');
        $username = input('username');
        $status = input('status');

        if ($id) {
            $where[] = ['id', '=', $id];
        }
        if ($username) {
            $where[] = ['username', '=', $username];
        }
        if ($status) {
            $where[] = ['status', '=', $status];
        }

        //URL参数
        $pageMaps = array();
        $pageMaps['id'] = $id;
        $pageMaps['username'] = $username;
        $pageMaps['status'] = $status;

        $pageAry = [
            'list_rows' => 15,
            'page' => input('page', 1),
            'path' => 'index',
            'query' => $pageMaps,
        ];

        //查询数据
        $list = model('User')->loadList($where, $pageAry);
        $this->assign('list', $list);
        $this->assign('_page', $list->render());
        $this->assign('pageMaps', $pageMaps);

        $this->fetch('index');
    }

    public function info()
    {
        $request = request();
        $id = input('id');
        $userModel = model('User');
        if ($request->method() == 'POST') {
            $status = false;
            if ($id) {
                $temp = [
                    'username' => input('username'),
                    'status' => input('status')
                ];

                if (input('post.password')){
                    if (empty(input('post.password2'))){
                        returnJson(0,'确认密码不能为空');
                    }
                    if (input('post.password2')!=input('post.password')){
                        returnJson(0,'两次密码不一致');
                    }
    				if (!preg_match('/^[a-zA-Z\d_]{6,}$/', input('post.password'))) {
    					returnJson(0,'请输入正确的密码格式');
                    }

                    $userpass = input('password');

                    $Random = new Random();
                    $salt = strtolower($Random::strnum(10));
                    $new_password = md5(md5($userpass) . $salt . $id);

                    $temp['userpass'] = $userpass;
                    $temp['salt'] = $salt;
                    $temp['password'] = $new_password;
                }
                
                $userModel->where('id',$id)->update($temp);
                $status = true;
            }
            if ($status !== false) {
                returnJson(1, '操作成功', ['url'=>url('user/index')]);
            } else {
                returnJson(0, '操作失败');
            }
        } else {
            $info = $userModel->getInfo($id);
            $this->assign('info', $info);

            $this->fetch('info');
        }
    }

    /**
     * 删除信息
     * @param int $id
     * @return bool 删除状态
     */
    public function del()
    {
        $id = input('id');
        if (empty($id)) {
            returnJson(0, '参数不能为空');
        }

        $userModel = model('User');
        if ($userModel->del($id)) {
            returnJson(1, '删除成功！');
        } else {
            returnJson(0, '删除失败');
        }
    }


}
